#include "utils.h"
#include <pthread.h>
#include <semaphore.h>

void *thread_func_static_sem(void *param) {
    long long t_id = (long long)param;
    for (int k = 0; k < n; k++) {
        if (t_id == 0) {
            float ele = A[k][k];
            for (int j = k + 1; j < n; j++) A[k][j] /= ele;
            A[k][k] = 1.0;
            for (int i = 0; i < thread_count - 1; i++) sem_post(sem_Division + i);
        } else {
            sem_wait(sem_Division + t_id - 1);
        }
        for (int i = k + t_id + 1; i < n; i += thread_count) {
            for (int j = k + 1; j < n; j++) A[i][j] -= A[i][k] * A[k][j];
            A[i][k] = 0;
        }
        if (t_id == 0) {
            for (int i = 0; i < thread_count - 1; i++) sem_wait(&sem_leader);
            for (int i = 0; i < thread_count - 1; i++) sem_post(sem_Elimination + i);
        } else {
            sem_post(&sem_leader);
            sem_wait(sem_Elimination + t_id - 1);
        }
    }
    pthread_exit(NULL);
    return NULL;
}

void static_gauss_sem() {
    // Implementation of Gaussian elimination with static thread and semaphore optimization
}
